package com.doublethread.easymart.modules.order.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 订单状态枚举
 */
@Getter
@AllArgsConstructor
public enum OrderStatus {

    /**
     * 已取消
     */
    CANCELLED(0, "已取消"),

    /**
     * 待支付
     */
    PENDING_PAYMENT(1, "待支付"),

    /**
     * 待发货
     */
    PENDING_DELIVERY(2, "待发货"),

    /**
     * 待收货
     */
    PENDING_RECEIVE(3, "待收货"),

    /**
     * 已完成
     */
    COMPLETED(4, "已完成"),

    /**
     * 已过期
     */
    EXPIRED(5, "已过期");

    /**
     * 状态码
     */
    private final Integer code;

    /**
     * 状态描述
     */
    private final String description;

    /**
     * 根据状态码获取枚举
     */
    public static OrderStatus getByCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (OrderStatus status : values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        return null;
    }

    /**
     * 判断是否可以取消
     */
    public boolean canCancel() {
        return this == PENDING_PAYMENT || this == PENDING_DELIVERY;
    }

    /**
     * 判断是否可以支付
     */
    public boolean canPay() {
        return this == PENDING_PAYMENT;
    }

    /**
     * 判断是否可以发货
     */
    public boolean canDeliver() {
        return this == PENDING_DELIVERY;
    }

    /**
     * 判断是否可以确认收货
     */
    public boolean canConfirm() {
        return this == PENDING_RECEIVE;
    }

    /**
     * 判断是否可以评价
     */
    public boolean canComment() {
        return this == COMPLETED;
    }

    /**
     * 判断是否为终态
     */
    public boolean isFinalStatus() {
        return this == CANCELLED || this == COMPLETED;
    }
}
